#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "shape.h"
#include "grammar.h"

#include <QDebug>
#include <QMouseEvent>
#include <QGraphicsPixmapItem>


void test1(QGraphicsScene * scene);
void printGrammar(QGraphicsScene * scene);


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{


    ui->setupUi(this);
    ui->graphicsView_3 ->setScene(new QGraphicsScene);
    ui->graphicsView_4 ->setScene(new QGraphicsScene);
    ui->graphicsView_3->setAlignment(Qt::AlignLeft);
    ui->graphicsView_4->setAlignment(Qt::AlignTop|Qt::AlignLeft);
    ui->graphicsView_4->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    ui->graphicsView_4->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);




    ui->tabWidget-> setTabText(0,"Gramatyka");
    ui->tabWidget-> setTabText(1,"Rysowanie");
    ui->tabWidget->setCurrentWidget(ui->tab_5);
    ui->graphicsView_4->installEventFilter(this);


    showMaximized();



    stan=new Stan(ui->graphicsView_4,ui->verticalLayout);
    printGrammar(ui->graphicsView_3->scene());



}

MainWindow::~MainWindow()
{
    delete ui;
    delete stan;
}


void test1(QGraphicsScene* scene)
{


    Shape s("pietro");
    QGraphicsPixmapItem *item;
    double scale=0.5;
    for(int i=0;i<30;++i)
    {
        item=scene->addPixmap(QPixmap::fromImage(s.image));
        item->setScale(scale);
        item->translate(0,item->boundingRect().height()*i);
    }


}


void printGrammar(QGraphicsScene* scene)
{


    Grammar g;



    QGraphicsPixmapItem *item;
    QGraphicsEllipseItem *ellipse;
    QImage strzalka("strzalka.png");



    double ypos=0.0;
    double isize=110.0;
    double d=20.0;

    for(int i=0;i<g.productions.size();++i,ypos+=2.15*isize)
    {

        Production p=g.productions.at(i);

        if (p.lshape.name==QString("rMarker"))
        {

            ellipse=scene->addEllipse(0,0,d,d);
            ellipse->setBrush(QBrush(QColor(0,0,0)));
            ellipse->translate(0.,ypos);
            item=scene->addPixmap(QPixmap::fromImage(strzalka));
            item->translate(isize,ypos+d/2-strzalka.height()/2);

        }
        else
        {
            item=scene->addPixmap(QPixmap::fromImage(p.lshape.image));
            item->translate(0.0,ypos);


            Marker u=p.lshape.uMarker;
            Marker r=p.lshape.rMarker;


            double x=isize;
            double y=ypos+p.lshape.image.height()/2.0-strzalka.height()/2;
            item=scene->addPixmap(QPixmap::fromImage(strzalka));
            item->translate(x,y);



            if(r.exist)
            {
                ellipse=scene->addEllipse(0,0,d,d);
                ellipse->setBrush(QBrush(QColor(0,0,0)));
                ellipse->translate(r.position.x()-d/2,ypos-r.position.y()-d/2);
            }

            if(u.exist)
            {
                ellipse=scene->addEllipse(0,0,d,d);
                ellipse->setBrush(QBrush(QColor(255,0,0)));
                ellipse->translate(u.position.x()-d/2,ypos-u.position.y()-d/2);

            }



        }

        //rysowanie prawych ksztaltow
        for(int j=0;j<p.rshape.positions.size();++j)
        {

            item=scene->addPixmap(QPixmap::fromImage(p.rshape.shapes.at(j).image));
            double y=ypos-p.rshape.positions.at(j).y();
            double x=2*isize+p.rshape.positions.at(j).x();
            item->translate(x,y);


        }
        //rysowanie markerow prawych ksztaltow
        for(int j=0;j<p.rshape.positions.size();++j)
        {
            double y=ypos-p.rshape.positions.at(j).y();
            double x=2*isize+p.rshape.positions.at(j).x();
            Marker u=p.rshape.shapes.at(j).uMarker;
            Marker r=p.rshape.shapes.at(j).rMarker;
            if(r.exist)
            {
                ellipse=scene->addEllipse(0,0,d,d);
                ellipse->setBrush(QBrush(QColor(0,0,0)));
                ellipse->translate(x+r.position.x()-d/2,y-r.position.y()-d/2);
            }

            if(u.exist)
            {
                ellipse=scene->addEllipse(0,0,d,d);
                ellipse->setBrush(QBrush(QColor(255,0,0)));
                ellipse->translate(x+u.position.x()-d/2,y-u.position.y()-d/2);

            }
        }

    }



}

bool MainWindow::eventFilter(QObject *obj, QEvent *event)
{


    if (obj == ui->graphicsView_4)
    {
        if (event->type() == QEvent::MouseButtonPress)
        {
            QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
            stan->sceneClicked(mouseEvent->posF());
            //qDebug()<<"object name: "<<obj->objectName();
            //qDebug()<<"click position: "<<mouseEvent->posF();
            return true;
        } else {
            return false;
        }
    }
    else
    {
        qDebug()<<obj->objectName();
        return MainWindow::eventFilter(obj,event);
    }

}




